import router from 'koa-router';
import RSATool from '../lib/RSATool';
import configTool from '../com/configTool';
import getServerInfo from '../lib/System/getServerInfo';
import Authentication from '../auth/Authentication';

/**
 * @swagger
 * tags:
 *   - name: 认证授权
 *     description: 用户认证和授权相关接口
 */
const AuthorizeRoutes = new router({prefix: '/AuthorizeServer'});

/**
 * @swagger
 * tags:
 *   - name: 认证授权
 *     description: 用户认证和授权相关接口
 */

AuthorizeRoutes
    /**
     * @swagger
     * /AuthorizeServer/login:
     *   get:
     *     tags: [认证授权]
     *     summary: 用户登录
     *     description: 使用用户名和密码进行系统登录，获取访问令牌
     *     parameters:
     *       - name: userName
     *         in: query
     *         description: 用户名
     *         required: true
     *         type: string
     *       - name: pwd
     *         in: query
     *         description: 密码（也可使用password参数名）
     *         required: true
     *         type: string
     *     responses:
     *       200:
     *         description: 登录成功
     *         schema:
     *           type: object
     *           properties:
     *             code:
     *               type: integer
     *               example: 200
     *             message:
     *               type: string
     *               example: "登录成功"
     *             data:
     *               type: object
     *               properties:
     *                 serverInfo:
     *                   type: object
     *                   description: 服务器基本信息
     *                 userInfo:
     *                   type: object
     *                   properties:
     *                     userName:
     *                       type: string
     *                       description: 用户名
     *                     ip:
     *                       type: string
     *                       description: 登录IP地址
     *                     token:
     *                       type: string
     *                       description: JWT访问令牌
     *                 license:
     *                   type: object
     *                   description: 许可证验证信息
     *       401:
     *         description: 登录失败
     *         schema:
     *           type: object
     *           properties:
     *             code:
     *               type: integer
     *               example: 401
     *             message:
     *               type: string
     *               example: "登录失败，用户名或密码错误"
     *             data:
     *               type: null
     */
    .get('/login', async (ctx: any) => {
        let userName = ctx.request.query?.userName;
        let pwd = ctx.request.query?.pwd || ctx.request.query?.password;

        const userInfo = configTool.baseConfig.userList.find((item: any) => item.userName === userName && item.password === pwd);

        ctx.set('content-type', 'application/json;charset=utf-8');
        if (userInfo) {
            const serverInfo = await getServerInfo();
            const userCopy: any = {...userInfo};

            delete userCopy.password;
            userCopy.ip = ctx.request.ip;
            userCopy.token = RSATool.createJWT(ctx.request.ip, configTool.baseConfig.privateDer);

            ctx.body = JSON.stringify({
                code: 200,
                message: '登录成功',
                data: {
                    serverInfo,
                    userInfo: userCopy,
                    license: await Authentication.Verification()
                }
            });
            ctx.status = 200;
        } else {
            ctx.body = JSON.stringify({
                code: 401,
                message: '登录失败，用户名或密码错误',
                data: null
            });
            ctx.status = 401;
        }
    });

export default AuthorizeRoutes;
